layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

	let year = new Date().getFullYear();//年
	let month = new Date().getMonth() + 1;//注意！月份是从0月开始获取的，所以要+1;

    laydate.render({
        elem: '#startTime',
 		format: 'yyyy-MM',
		type:'month',
		value:year + '-' + (month >= 10 ? month : '0' + month)
    });

    /**
     * 调度单表管理
     */
    var TDispatch = {
        tableId: "tDispatchTable"
    };

	var _data = []
	reds(year + '-' + (month >= 10 ? month : '0' + month));
	
	function reds(month){
		$.ajax({
			type: 'get',
			data: {month:month,operateId:$("#operateId").val(),deliverId:1},
			url: "/panelTms2/monthTotalindex2",
			async: false,
			dataType:"json",
			success: function (data) {
				if(data.data.length == 0){
    				yer.msg('未查询到数据'); 
					return
				}
				let arr = data.data
				_data = []
				for (var i = 0; i < arr.yDXLZLFXs.length ; i++) {
					var obj1 = {}
					obj1.xianlu = arr.yDXLZLFXs[i].xL
					obj1.zfs = (arr.yDXLZLFXs[i].zFS*1).toFixed(2)
					obj1.zyf = (arr.yDXLZLFXs[i].zYF*1).toFixed(2)
					obj1.ckj = 0
					obj1.pjj = 0
					obj1.ckj2 = 0
					obj1.pjj2 = 0
					_data.push(obj1)
				}
				
				for (var i = 0; i < _data.length ; i++) {
					for (var j = 0; j < arr.yDXLCBJFFDFXs.length ; j++) {
						if(_data[i].xianlu == arr.yDXLCBJFFDFXs[j].xL){
							_data[i].ckj = (arr.yDXLCBJFFDFXs[j].cKJ*1).toFixed(2)
							_data[i].pjj = (arr.yDXLCBJFFDFXs[j].pJJ*1).toFixed(2)
							continue
						}
					}
				}
				for (var i = 0; i < _data.length ; i++) {
					for (var j = 0; j < arr.yDXLCBDCFDFXs.length ; j++) {
						if(_data[i].xianlu == arr.yDXLCBDCFDFXs[j].xL){
							_data[i].ckj2 = (arr.yDXLCBDCFDFXs[j].cKJ*1).toFixed(2)
							_data[i].pjj2 = (arr.yDXLCBDCFDFXs[j].pJJ*1).toFixed(2)
							continue
						}
					}
				}
			}
		})
	
    
	    /**
	     * 初始化表格的列
	     */
	    TDispatch.initColumn = function () {
	        return [[
	            {type: 'checkbox',rowspan:2},
	            {field: 'xianlu', title: '线路名称' ,rowspan:2, align: "center", totalRowText: '合计：'},
	            {field: '', title: '月度线路总量',colspan:2, align: "center"},
	            {field: '', title: '月度线路成本均方浮动',colspan:2, align: "center"},
	            {field: '', title: '月度线路成本吨车浮动',colspan:2, align: "center"},
	        ],[
	            {field: 'zfs', title: '总方数', align: "center", totalRow: true, sort: true},
	            {field: 'zyf', title: '总运费', align: "center", totalRow: true, sort: true},
	            {field: 'ckj', title: '参考价', align: "center", sort: true},
	            {field: 'pjj', title: '平均价', align: "center", sort: true},
	            {field: 'ckj2', title: '参考价', align: "center", sort: true},
	            {field: 'pjj2', title: '平均价', align: "center", sort: true},
	        ]];
	    };
	    
	    

	    // 渲染表格
	    var tableResult = table.render({
	        elem: '#' + TDispatch.tableId,
	        data:_data,
	        page: false,
	        limit: 1000,
	        height: "full-60",
	        totalRow: true,
	        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
	        defaultToolbar: ['filter', 'exports', 'print'],
	        cellMinWidth: 100,
	        cols: TDispatch.initColumn()
	    });
	}

    /**
     * 导出excel按钮
     */
    TDispatch.exportExcel = function () {
        var checkRows = table.checkStatus(TDispatch.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 导出excel
    $('#btnExp').click(function () {
        TDispatch.exportExcel();
    });

    /**
     * 点击查询按钮
     */
    TDispatch.search = function () {
		reds($("#startTime").val())
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TDispatch.search();
    });

    // 单击行事件
    table.on('row(' + TDispatch.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + TDispatch.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + TDispatch.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });
    
});
